package com.hanter.android.radui.mvp;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.hanter.android.radui.R;

public abstract class MvpActivity<V extends BaseView, T extends BasePresenter<V>>
        extends BaseActivity implements BaseView, View.OnClickListener {

    protected T presenter;
    protected FrameLayout contentContainer;
    protected View contentView;
    private ContentLoadStatusViewHelper contentStatusViewHelper;
    private LoadingDialogHelper loadingDialogHelper;
    private boolean showLoading;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initBaseContentView();
        presenter = createPresenter();
        //noinspection unchecked
        presenter.attachView((V) this);

        loadingDialogHelper = new LoadingDialogHelper(getSupportFragmentManager());
    }

    /**
     * 加载框、无数据、主界面三种状态的处理
     */
    protected void initBaseContentView() {
        contentContainer = findViewById(android.R.id.content);
        // 添加主视图布局
        createAddContentView();
        contentStatusViewHelper = createContentViewHelper(contentContainer, contentView);
        contentStatusViewHelper.initViews(showLoading);
    }

    protected ContentLoadStatusViewHelper createContentViewHelper(ViewGroup contentContainer, View realContentView) {
        return new ContentLoadStatusViewHelper(this, contentContainer, realContentView);
    }

    /**
     * 主界面
     */
    private void createAddContentView() {
        contentView = inflate(getLayout(), contentContainer);
        if (showLoading) {
            contentView.setVisibility(View.INVISIBLE);
        }
        setContentView(contentView);
    }

    @LayoutRes
    protected abstract int getLayout();

    public View inflate(@LayoutRes int resId, ViewGroup parent) {
        return LayoutInflater.from(this).inflate(resId, parent, false);
    }

    /**
     * 必须在onCreate之前调用（否则不会显现加载框）
     *
     * @param showLoading 是否显示加载
     */
    public void setShowContentLoading(boolean showLoading) {
        this.showLoading = showLoading;
    }

    @Override
    public Context getContext() {
        return this;
    }

    public abstract T createPresenter();

    @Override
    public void showProgressDialog() {
        loadingDialogHelper.showProgressDialog();
    }

    @Override
    public void disProgressDialog() {
        loadingDialogHelper.dismissProgressDialog();
    }

    @Override
    public void hideContentView() {
        contentStatusViewHelper.hideContentView();
    }

    @Override
    public void showContentView() {
        contentStatusViewHelper.showContentView();
    }

    @Override
    public void showNoData() {
        contentStatusViewHelper.showNoDataView();
    }

    @Override
    public void hideNoData() {
        contentStatusViewHelper.hideExceptionView();
    }

    @Override
    public void showPageFault() {
        contentStatusViewHelper.showPageFaultView();
    }

    @Override
    public void showNoNetWork() {
        contentStatusViewHelper.showNoNetworkView();
    }

    @Override
    public void showLoadingView() {
        contentStatusViewHelper.showLoadingView();
    }

    @Override
    public void completeLoading() {
        contentStatusViewHelper.completeLoading();
    }

    @Override
    public void navBack() {
        onBackPressed();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.leftBar) {
            onBackPressed();
        } else if (id == R.id.btnContentExceptionRefresh) {
            showLoadingView();
            onContentRefreshClick();
        }
    }

    protected void onContentRefreshClick() {

    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        if (presenter != null)
            presenter.detachView();
        disProgressDialog();
        super.onDestroy();
    }
}
